package com.redside.dao.hibernate;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;

import com.redside.dao.BusinessDao;
import com.redside.model.Business;
import com.redside.view.BusinessAssociatorView;

/**
 * This class interacts with Spring and Hibernate to save and retrieve Business
 * objects.
 * 
 * @author <a href="mailto:pchapman@easystreet.com">Peter Chapman</a>
 */
public class BusinessDaoHibernate extends GenericDaoHibernate<Business, Long>  implements BusinessDao {

    public BusinessDaoHibernate() {
        super(Business.class);
    }
    
    @SuppressWarnings("unchecked")
    public List<Business> findByCountyId(String id) {
        return getHibernateTemplate().find("from Business where address.countyId=?", id);
    }
    
    @SuppressWarnings("unchecked")
    public List<Business> findByCountyIdSorted(String countyId, String[] sortOrder){
        Criteria criteria = getSession().createCriteria(Business.class);
        //criteria.add(Expression.eq("active", true));
        logger.debug("======> county: "+countyId);
        if(!StringUtils.equals(countyId,"0"))
            criteria.add(Expression.eq("address.countyId", countyId));
        for(int i=0; i<sortOrder.length;i++){
            criteria.addOrder(Order.asc(sortOrder[i]));
        }
        return criteria.list();
    }

    @SuppressWarnings("unchecked")
    public List<Business> findByAssociation(BusinessAssociatorView view) {
        Criteria criteria = getSession().createCriteria(Business.class);
        if (view.isCheckBusinessName()) {
            logger.debug("INCLUDING NAME IN SEARCH..."+view.getBusiness().getBusinessName());
            if(view.getBusinessMatchMode().equals("starts"))
                criteria.add(Expression.like("businessName", view.getBusiness().getBusinessName().trim(), MatchMode.START).ignoreCase());
            if(view.getBusinessMatchMode().equals("ends"))
                criteria.add(Expression.like("businessName", view.getBusiness().getBusinessName().trim(), MatchMode.END).ignoreCase());
            if(view.getBusinessMatchMode().equals("contains"))
                criteria.add(Expression.like("businessName", view.getBusiness().getBusinessName().trim(), MatchMode.ANYWHERE).ignoreCase());
            if(view.getBusinessMatchMode().equals("matches"))
                criteria.add(Expression.like("businessName", view.getBusiness().getBusinessName().trim(), MatchMode.EXACT).ignoreCase());
        }
        if (view.isCheckBusinessAddress1()) {
            logger.debug("INCLUDING ADDRESS1 IN SEARCH..."+view.getBusiness().getAddress().getAddress1());
            if(view.getAddressMatchMode().equals("starts"))
                criteria.add(Expression.like("address.address1", view.getBusiness().getAddress().getAddress1().trim(), MatchMode.START).ignoreCase());
            if(view.getAddressMatchMode().equals("ends"))
                criteria.add(Expression.like("address.address1", view.getBusiness().getAddress().getAddress1().trim(), MatchMode.END).ignoreCase());
            if(view.getAddressMatchMode().equals("contains"))
                criteria.add(Expression.like("address.address1", view.getBusiness().getAddress().getAddress1().trim(), MatchMode.ANYWHERE).ignoreCase());
            if(view.getAddressMatchMode().equals("matches"))
                criteria.add(Expression.like("address.address1", view.getBusiness().getAddress().getAddress1().trim(), MatchMode.EXACT).ignoreCase());            
        }
        if (view.isCheckBusinessAddress2() && !view.getBusiness().getAddress().getAddress2().trim().equals("")) {
            logger.debug("INCLUDING ADDRESS2 IN SEARCH...");
            criteria.add(Expression.like("address.address2", view.getBusiness().getAddress().getAddress2().trim(), MatchMode.ANYWHERE).ignoreCase());
        }
        if (view.isCheckBusinessCity() && !view.getBusiness().getAddress().getCity().trim().equals("")) {
            logger.debug("INCLUDING CITY IN SEARCH..."+view.getBusiness().getAddress().getCity());
            criteria.add(Expression.like("address.city", view.getBusiness().getAddress().getCity().trim(), MatchMode.ANYWHERE).ignoreCase());
        }
        if (view.isCheckBusinessZip() && !view.getBusiness().getAddress().getPostalCode().trim().equals("")) {
            logger.debug("INCLUDING ZIP IN SEARCH...");
            criteria.add(Expression.like("address.postalCode", view.getBusiness().getAddress().getPostalCode().trim(), MatchMode.ANYWHERE));
        }
        if(view.isCheckBusinessPhone() && !view.getBusiness().getBusinessPhone().trim().equals("")){
            logger.debug("INCLUDING PHONE IN SEARCH..."+view.getBusiness().getBusinessPhone());
            criteria.add(Expression.like("businessPhone", view.getBusiness().getBusinessPhone().trim(), MatchMode.ANYWHERE));
        }
        logger.debug("Here is the countyId: "+view.getBusiness().getAddress().getCountyId());
        logger.debug("Here is the countyId.toString: "+view.getBusiness().getAddress().toString());
        criteria.add(Expression.eq("address.countyId", view.getBusiness().getAddress().getCountyId()));
        criteria.add(Expression.eq("active", true));
        criteria.addOrder( Order.asc("businessName").ignoreCase());
        List<Business> businesses = criteria.list();
        return businesses;
    }

}
